//
//  EditeNotAppViewController.m
//  IconMaster
//
//  Created by mac on 2017/7/25.
//  Copyright © 2017年 yangweiwei. All rights reserved.
//

#import "EditeNotAppViewController.h"
#import <Contacts/Contacts.h>
#import <ContactsUI/ContactsUI.h>
#import "CImageUtil.h"
#import "CIconView.h"
#import "MongooseDaemon.h"
#import "RMStepsController.h"
#import "EditeAppViewController.h"
@interface EditeNotAppViewController ()<CNContactPickerDelegate>
@property (nonatomic, strong) NSDictionary *inputDict;
@property (weak, nonatomic) IBOutlet UITextField *inputTextFeild;
@property (weak, nonatomic) IBOutlet UITextField *editeTextFeild;
@property (weak, nonatomic) IBOutlet UIButton *adressBookBtn;
@property (weak, nonatomic) IBOutlet UIButton *confirmbtn;
@property (weak, nonatomic) IBOutlet UIView *adView;

@property (nonatomic, strong) NSString *isTypeString;

@end

@implementation EditeNotAppViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.imgView0.image = self.customerImg;
    self.imgView0.layer.cornerRadius = 8;
    self.imgView0.layer.masksToBounds = YES;
    if ([self.isFromClickStr isEqualToString:Telephone] || [self.isFromClickStr isEqualToString:SmsMessage]) {
        //
    }else{
        [self.adressBookBtn setHidden: YES];
        self.inputTextFeild.placeholder = NSLocalizedString(@"请输入名称","");
        self.editeTextFeild.placeholder = NSLocalizedString(@"请输入网址或者邮箱","");
        self.inputTextFeild.sd_layout.widthRatioToView(self.editeTextFeild, 1);
        
        
    
    }
    // Do any additional setup after loading the view.
}
- (IBAction)adressBookBtnAction:(id)sender {
    
    // 1.创建选择联系人的控制器
    CNContactPickerViewController *contactVc = [[CNContactPickerViewController alloc] init];
    // 2.设置代理
    contactVc.delegate = self;
    // 3.弹出控制器
    [self presentViewController:contactVc animated:YES completion:nil];
}
/*
 self.prefixDict = [NSMutableDictionary dictionary];
	[self.prefixDict setValue:@"tel:" forKey:KEY_CALL];
	[self.prefixDict setValue:@"sms:" forKey:KEY_SMS];
	[self.prefixDict setValue:@"mailto:" forKey:KEY_EMAIL];
	[self.prefixDict setValue:@"http://" forKey:KEY_WEB];
 */
- (IBAction)confirmBtnAction:(id)sender {
    
//    [self creatIconWithMessage:@{@"iconImage":self.customerImg,
//                                 @"title":self.inputTextFeild.text,
//                                 @"value":[NSString stringWithFormat:@"%@%@", @"tel:", self.editeTextFeild.text]}];
    if ([self.inputTextFeild.text length] == 0 || [self.editeTextFeild.text length] == 0) {
        [self alterAction: NSLocalizedString(@"请填写完整信息","")];
    }else{
        self.inputDict = @{@"iconImage":self.customerImg,
                           @"title":self.inputTextFeild.text,
                           //                       @"iconView":image1,
                           @"value":[NSString stringWithFormat:@"%@%@", self.isFromClickStr, self.editeTextFeild.text]/*@"https://itunes.apple.com/us/app/qq/id444934666?mt=8"*/};
        //    self.inputDict = dict;
        UIImage* iconImage = [self.inputDict objectForKey:@"iconImage"];
        SaveImageToDocument(iconImage, @"the_icon.png");
        NSString* title = [self.inputDict objectForKey:@"title"];
        NSString* value = [self.inputDict objectForKey:@"value"];
        
        [MongooseDaemon instance].composedUrl.title = title;
        [MongooseDaemon instance].composedUrl.password = nil;
        [MongooseDaemon instance].composedUrl.realUrl = value;
        
        NSString* urlString = [NSString stringWithFormat:@"http://localhost:8080/wei%ld",time(NULL)];
        //    NSLog(@"%@",urlString);
        //    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{}   completionHandler:^(BOOL success) {
            if (success) {
                NSLog(@"Opened url");
                
            }
        }];
        [self.stepsController finishedAllSteps];

    }
       
}
// 1.点击取消按钮调用的方法
- (void)contactPickerDidCancel:(CNContactPickerViewController *)picker
{
    NSLog(@"取消选择联系人");
}
// 2.当选中某一个联系人时会执行该方法
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact
{
    // 1.获取联系人的姓名
    NSString *lastname = contact.familyName;
    NSString *firstname = contact.givenName;
    NSLog(@"%@ %@", lastname, firstname);
    
    // 2.获取联系人的电话号码(此处获取的是该联系人的第一个号码,也可以遍历所有的号码)
    NSArray *phoneNums = contact.phoneNumbers;
    CNLabeledValue *labeledValue = phoneNums[0];
    CNPhoneNumber *phoneNumer = labeledValue.value;
    NSString *phoneNumber = phoneNumer.stringValue;
    NSLog(@"%@", phoneNumber);
    
    self.inputTextFeild.text = [NSString stringWithFormat:@"%@%@", lastname,firstname];
    self.editeTextFeild.text = [NSString stringWithFormat:@"%@", phoneNumber];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
